﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Collections.Generic;
using DataAccess.DataObject;

namespace EnmineKaitlin.Common
{
    public partial class ajaxProcess : PageBase
    {
        string method = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request["method"] != null && Request["method"] != "")
                {
                    method = Request["method"].ToString();
                    switch (method)
                    {
                        case "deleteoption":
                            if (Request["OptionID"] != null && Request["OptionID"] != "")
                            {
                                if (!_BOption.Delete(int.Parse(Request["OptionID"].ToString())))
                                  Response.Write("false");
                            }
                            break;
                        case "addoption":
                            if (Request["OptionName"] != null && Request["OptionName"] != "" && Request["PollID"]!=null && Request["PollID"].ToString()!="")
                            {
                                int PollID = int.Parse(Request["PollID"].ToString());
                                string OptionName = Request["OptionName"].ToString();
                                _OOption.PollID = PollID;
                                _OOption.OptionName = OptionName;
                                _OOption.OptionParent = 0;
                                _OOption.OptionValue = "";
                                _OOption.Description = "";
                                if (_BOption.Add(_OOption))
                                {
                                    int OptionID = _BOption.GetLastRecord()[0].OptionID;
                                    Response.Write("<p id='p" + OptionID + "' style='overflow:hidden;'><span  style='float:left;'>&bull;&nbsp;&nbsp;" + OptionName + "&nbsp;&nbsp;&nbsp;</span><a class='deleteoption btnDelete' id='" + OptionID + "' href='#' title='Xóa'>Xóa</a></p>");
                                }
                            }
                            break;
                        case "vote":
                            if (Request["OptionID"] != null && Request["OptionID"] != "")
                            {
                                if(_BOption.Vote(int.Parse(Request["OptionID"].ToString())))
                                {
                                    string html = "";
                                    if (Request["PollID"] != null && Request["PollID"].ToString() != "")
                                    {                                        
                                        int PollID = int.Parse(Request["PollID"].ToString());
                                        IList<OOption> lstOption = _BOption.Get(PollID);
                                        int TotalVote = 0;
                                        foreach (OOption objOption in lstOption) {
                                            TotalVote += objOption.Vote;
                                        }
                                        foreach (OOption objOption in lstOption)
                                        {
                                            double percent = (double.Parse(objOption.Vote.ToString()) / double.Parse(TotalVote.ToString())) * 100;
                                            percent = Math.Round(percent);
                                            html += "<div class='item-option'>";
                                            html += "<span>" + objOption.OptionName + ":</span>";
                                            html += "<div class='bar-container'><div class='bar' style='width:" + percent.ToString() + "%'>&nbsp;</div></div>";
                                            html += "<strong>" + percent.ToString() + "%</strong>";
                                            html += "</div>";
                                        }
                                        html += "<p class='total'>Tổng bình chọn: <strong>" + TotalVote + "</strong></p>";
                                    }
                                    html += "<p><input type='button' value='Bình chọn' class='btn btnVoteBack' id='btnVote'></p>";
                                    Response.Write(html);
                                }
                            }
                            break;
                        case "result":
                            if (Request["OptionID"] != null && Request["OptionID"] != "" && Request["PollID"] != null && Request["PollID"].ToString() != "")
                            {
                                string html = "";                                    
                                int PollID = int.Parse(Request["PollID"].ToString());
                                IList<OOption> lstOption = _BOption.Get(PollID);
                                int TotalVote = 0;
                                foreach (OOption objOption in lstOption)
                                {
                                    TotalVote += objOption.Vote;
                                }
                                foreach (OOption objOption in lstOption)
                                {
                                    double percent = (double.Parse(objOption.Vote.ToString()) / double.Parse(TotalVote.ToString())) * 100;
                                    percent = Math.Round(percent);                 
                                    html += "<div class='item-option'>";
                                    html += "<span>" + objOption.OptionName + ":</span>";
                                    html += "<div class='bar-container'><div class='bar' style='width:" + percent.ToString() + "%'>&nbsp;</div></div>";
                                    html += "<strong>" + percent.ToString() + "%</strong>";
                                    html += "</div>";
                                }
                                html += "<p class='total'>Tổng bình chọn: <strong>"+TotalVote+"</strong></p>";
                                html += "<p><input type='button' value='Bình chọn' class='btn btnVoteBack' id='btnVote'></p>";
                                Response.Write(html);                                
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            { 
                
            }
            Response.End();
        }        
    }
}
